/*
 * 文件名： BmsVoltageInfo.java
 * 
 * 创建日期： 2016年12月19日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */
package com.moli.iov.protocol.model.realtime;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Arrays;

import com.moli.iov.protocol.codec.Serialize;
import com.moli.iov.protocol.model.EntityBytesLen;
import com.moli.iov.util.ByteArrayUtil;


/**
 * 可充电储能子系统信息实体
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年12月19日
 */
@Serialize(unit="byte",isBigEndian=true)
public class BmsVoltageInfo implements Serializable,EntityBytesLen{

	/** 
	 */ 
	private static final long serialVersionUID = 1L;

	/**
	 * 可充电储能子系统号
	 * 
	 * 有效值范围：0～250
	 */
	@Serialize(unit="byte",offset=0,size=1,min=0,max=250)
	public short storageSubSystemCode;

	/**
	 * 可充电储能装置电压
	 * 
	 * 有效值范围：0～10 000（表示0～1000V）,最小计量单元:0.1V
	 * 
	 */
	@Serialize(unit="byte",offset=1,size=2, coefficient=0.1, max=10000, min=0)
	public double storageVoltage;

	/**
	 * 可充电储能装置电流
	 * 有效值范围：0～20 000（数值偏移量1 000A,表示-1000A～+1000A）,
	 * 最小计量单元:0.1A, "0xFE"表示异常，"0xFF"表示无效。
	 * 
	 */
	@Serialize(unit="byte",offset=3,size=2, coefficient=0.1,valOffset=1000, max=20000, min=0)
	public double storageCurrent;

	/**
	 * 单体电池总算
	 * N个电池单体，有效值范围：1～65531，
	 * "0xFE"表示异常，"0xFF"表示无效。
	 * 
	 */
	@Serialize(unit="byte",offset=5,size=2, max=65531, min=1)
	public int batteryCellFinally;

	/**
	 * 
	 * 本帧起始电池序号
	 * 当本帧单体个数超过200时，应拆分成多帧数据进行传输，
	 * 有效值范围：1～65531
	 * 
	 */
	@Serialize(unit="byte",offset=7,size=2, max=65531, min=1)
	public int batteryCellStartSernum;

	/**
	 * 
	 *	本帧单体电池总数
	 *  本帧单体总数m；有效值范围：1～200
	 * 
	 */
	@Serialize(unit="byte",offset=9,size=1, max=300, min=1)
	public int batteryCellSum;

	/**
	 * 
	 * 有效值范围：0～60 000（数值偏移量0V0～60. 000V,表示-1000A～+1000A）,
	 * 最小计量单元:0.001V,单体电池电压个数等于本帧单体电池总数m, 
	 * "0xFE"表示异常，"0xFF"表示无效。
	 * 
	 */
	public double[] batteryCellVoltage;

	public double[] getBatteryCellVoltage() {
		return batteryCellVoltage;
	}

	public String getBatteryCellVoltageToString(){
		StringBuilder sBuilder = new StringBuilder();
		if(batteryCellVoltage!=null && batteryCellVoltage.length>0){
			for(double s : batteryCellVoltage){
				sBuilder.append(s+",");
			}
			return sBuilder.toString().substring(0,sBuilder.length()-1);
		}
		return null;

	}
	/**
	 * 报文长度
	 */
	private int bytesLen = 10;

	public void setBatteryCellVoltage(double[] batteryCellVoltage) {
		this.batteryCellVoltage = batteryCellVoltage;
	}

	@Serialize(unit="byte",offset=10,size=-1, coefficient=0.001, max=60000, min=0)
	public void setBatteryCellVoltage(byte[] batteryCellVoltage) {
		// 获取sysCode偏移量和长度
		int currentOffset = 0;
		int currentSize = 0;
		currentSize= this.batteryCellSum * 2;
		bytesLen +=currentSize ;
		byte[] currnetBytes = new byte[currentSize];
		this.batteryCellVoltage = new double[batteryCellSum];
		System.arraycopy(batteryCellVoltage, currentOffset, currnetBytes, 0, currentSize);
		byte[] twoInfBytes = new byte[2];
		for(int i=0;i<currentSize;i=i+2){
			System.arraycopy(currnetBytes, i, twoInfBytes, 0, 2);
			int intVol = ByteArrayUtil.toInt(twoInfBytes);
			double doubleVol = intVol * 0.001;
			this.batteryCellVoltage[i/2] = BigDecimal.valueOf(doubleVol).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
	}

	@Serialize(unit="byte",offset=10,size=-1, coefficient=0.001, max=60000, min=0)
	public byte[] getBatteryCellVoltage1() {
		int currentSize = this.batteryCellSum * 2;
		byte[] currnetBytes = new byte[currentSize];
		if(batteryCellVoltage!=null&&batteryCellVoltage.length>0){
			int i = 0;
			for(double d:batteryCellVoltage){
				byte[] bytes = new byte[2];
				if(d*1000>60000){
					bytes = new byte[]{(byte)0xff,(byte)0xff};
				}else{
					bytes = ByteArrayUtil.toBytes((short)(d*1000));
				}
				currnetBytes[i] = bytes[0];
				currnetBytes[i+1] = bytes[1];
				i = i+2;
			}
		}
		return currnetBytes;
	}

	@Override
	public int getBytesLen() {
		return bytesLen;
	}

	/**
	 * @return the storageSubSystemCode
	 */
	public short getStorageSubSystemCode() {
		return storageSubSystemCode;
	}

	/**
	 * @param storageSubSystemCode the storageSubSystemCode to set
	 */
	public void setStorageSubSystemCode(short storageSubSystemCode) {
		this.storageSubSystemCode = storageSubSystemCode;
	}

	/**
	 * @return the storageVoltage
	 */
	public double getStorageVoltage() {
		return storageVoltage;
	}

	/**
	 * @param storageVoltage the storageVoltage to set
	 */
	public void setStorageVoltage(double storageVoltage) {
		this.storageVoltage = storageVoltage;
	}

	/**
	 * @return the storageCurrent
	 */
	public double getStorageCurrent() {
		return storageCurrent;
	}

	/**
	 * @param storageCurrent the storageCurrent to set
	 */
	public void setStorageCurrent(double storageCurrent) {
		this.storageCurrent = storageCurrent;
	}

	/**
	 * @return the batteryCellFinally
	 */
	public int getBatteryCellFinally() {
		return batteryCellFinally;
	}

	/**
	 * @param batteryCellFinally the batteryCellFinally to set
	 */
	public void setBatteryCellFinally(int batteryCellFinally) {
		this.batteryCellFinally = batteryCellFinally;
	}

	/**
	 * @return the batteryCellStartSernum
	 */
	public int getBatteryCellStartSernum() {
		return batteryCellStartSernum;
	}

	/**
	 * @param batteryCellStartSernum the batteryCellStartSernum to set
	 */
	public void setBatteryCellStartSernum(int batteryCellStartSernum) {
		this.batteryCellStartSernum = batteryCellStartSernum;
	}

	/**
	 * @return the batteryCellSum
	 */
	public int getBatteryCellSum() {
		return batteryCellSum;
	}

	/**
	 * @param batteryCellSum the batteryCellSum to set
	 */
	public void setBatteryCellSum(int batteryCellSum) {
		this.batteryCellSum = batteryCellSum;
	}

	/**
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("BmsVoltageInfo [storageSubSystemCode=");
		builder.append(storageSubSystemCode);
		builder.append(", storageVoltage=");
		builder.append(storageVoltage);
		builder.append(", storageCurrent=");
		builder.append(storageCurrent);
		builder.append(", batteryCellFinally=");
		builder.append(batteryCellFinally);
		builder.append(", batteryCellStartSernum=");
		builder.append(batteryCellStartSernum);
		builder.append(", batteryCellSum=");
		builder.append(batteryCellSum);
		builder.append(", batteryCellVoltage=");
		builder.append(Arrays.toString(batteryCellVoltage));
		builder.append(", bytesLen=");
		builder.append(bytesLen);
		builder.append("]");
		return builder.toString();
	}
}
